What is the First In First Out (FIFO) algorithm? explain it with an example in OS.
Explain the First In First Out (FIFO) algorithm with an example in OS.
1079
31-Mar-2023
Updated on 20-Apr-2023
Aryan Kumar
20-Apr-2023The First In First Out (FIFO) algorithm is a scheduling algorithm used by operating systems to manage the execution of processes in the system. In this algorithm, the processes are executed in the order in which they arrive in the ready queue. The process that arrives first is executed first, and so on. This is similar to a queue data structure where the first element that is added to the queue is the first element that is removed.
Here is an example of how the FIFO algorithm works in an operating system:
Assume there are four processes P1, P2, P3, and P4 that need to be executed, and they arrive in the following order:
Each process requires a certain amount of time to complete its execution. Assume that the execution times for each process are:
To execute these processes using the FIFO algorithm, the operating system places them in a ready queue in the order in which they arrive. The first process in the queue, P1, is executed for 5 units of time, and then it completes its execution. The next process in the queue, P2, is then executed for 3 units of time, and so on. The execution sequence for the processes using the FIFO algorithm is as follows:
Krishnapriya Rajeev
31-Mar-2023The First In First Out (FIFO) algorithm is also used in page replacement in operating systems where the page that was brought into memory first should be the one that is replaced first.
Let's take an example to understand the FIFO page replacement algorithm. Let's consider a page reference string consisting of 10 pages:
3, 1, 4, 2, 5, 3, 2, 4, 6, 7
The size of the page frame is 3, which means that only 3 pages can be kept in memory at a time. Initially, the page frames are empty.
When the first page, 3, is referenced, it is not in memory, so a page fault (miss) occurs, and the page is brought into the first frame. When the second page, 1, is referenced, it is also not in memory, so it is brought into the second frame. Similarly, when the next few pages are referenced, they are brought into the next available frames until all the frames are filled. The page frame at this point looks like this:
Now, when the 2 page is referenced, it is not in memory, so the first page that was brought in, which is 3, is replaced by 2. The page frame now looks like this:
Similarly, when the next few pages are referenced, they are brought into the next available frames until all the frames are filled.
When the last page, 7, is referenced, it is not in memory, so the last page that was brought in, which is 3, is replaced by 7. The final page frame looks like this:
The advantage of the FIFO page replacement algorithm is it's simple and easy to implement.